package de.lulusoft.anothertorrent.gui.views.Providers;

import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;

import de.lulusoft.anothertorrent.core.rtorrent.Torrent;
import de.lulusoft.anothertorrent.gui.AnotherTorrentGui;
import de.lulusoft.anothertorrent.gui.util.Util;

public class TorrentListViewLabelProvider implements ITableLabelProvider {
	
	private static Image stopImage = null;
	private static Image startImage = null;
	private static Image hashingImage = null;
	private static Image leechingImage = null;
	private static Image seedingImage = null;

	@Override
	public void removeListener(ILabelProviderListener listener) {
	}

	@Override
	public boolean isLabelProperty(Object element, String property) {
		return false;
	}

	@Override
	public void dispose() {
	}

	@Override
	public void addListener(ILabelProviderListener listener) {
	}

	@Override
	public String getColumnText(Object element, int columnIndex) {
		if (element instanceof Torrent)
		{
			Torrent t = (Torrent)element;
			switch (columnIndex)
			{
			case 0: return t.getName();
			case 1: 
				{
					if (t.getHashing() > 0L)
						return "Hashing";						
					else
					{ // seeding or leeching or just started
						if (t.getPeers_connected() > 0L
								&& t.getConnection_current().equals(t.getConnection_leech()))
							return "Leeching";							
						else if (t.getPeers_connected() > 0L
								&& t.getConnection_current().equals(t.getConnection_seed()))
							return "Seeding";							
						else if (t.getState() == 1L)
							return "Started";
						else
							return "Stopped";
					}
				}
			case 2: 
				{
					if (t.getHashing() > 0L)
						return t.getDoneHashedPercent().toString() + " %";
					else
						return t.getDonePercent().toString() + " %";
				}
			case 3: return Util.formatBytes(t.getRemainingBytes());
			case 4: return Util.formatBytes(t.getSize_bytes());
			case 5: return Util.formatBytes(t.getDown_rate());
			case 6: return Util.formatBytes(t.getUp_rate());
			case 7: return Util.formatBytes(t.getUp_total());
			case 8: return t.getRatioFormated();
			case 9: return t.getPeers_connected().toString() + " (" + 
				t.getPeers_accounted() + "/" + t.getPeers_complete() + 
				"/" + t.getPeers_not_connected() + ")";
			case 10: return t.getPriority_str().toString();
			case 11: return Util.formatSeconds(t.getETA());
			case 12: return Util.formatBoolean(t.isPrivat());
			case 13: return Util.formatBoolean(t.getPeer_exchange());
			case 14: return t.getConnection_current();
			
			default: return "Not yet implemented";
			}
		}
		return "";
	}

	@Override
	public Image getColumnImage(Object element, int columnIndex) {
		if (element instanceof Torrent)
		{
			Torrent t = (Torrent)element;
			switch (columnIndex)
			{
			case 1: 
			{
				if (t.getState() == 0L)
				{
					if (stopImage == null)
						stopImage = AnotherTorrentGui.createImage("stop.png");
					return stopImage;
				}
				else if (t.getState() == 1L)
				{
					if (t.getHashing() > 0L)
					{ // hashing
						if (hashingImage == null)
							hashingImage = AnotherTorrentGui.createImage("arrow_refresh.png");
						return hashingImage;						
					}
					else
					{ // seeding or leeching or just started
						if (t.getPeers_connected() > 0L
								&& t.getConnection_current().equals(t.getConnection_leech()))
						{ // leeching
							if (leechingImage == null)
								leechingImage = AnotherTorrentGui.createImage("control_play_down_green.png");
							return leechingImage;							
						}
						else if (t.getPeers_connected() > 0L
								&& t.getConnection_current().equals(t.getConnection_seed()))
						{ // seeding
							if (seedingImage == null)
								seedingImage = AnotherTorrentGui.createImage("control_play_up_red.png");
							return seedingImage;							
						}
						else
						{ // just active
							if (startImage == null)
								startImage = AnotherTorrentGui.createImage("control_play_blue.png");
							return startImage;
						}
					}
					
				}
			}break;
			default: return null;
			}
		}
		return null;
	}
	
	/**
	 * Code from http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/Rotateandflipanimage.htm
	 * @param srcData
	 * @param direction
	 * @return
	 */
	 static ImageData rotate(ImageData srcData, int direction) 
	 {
		int bytesPerPixel = srcData.bytesPerLine / srcData.width;
		int destBytesPerLine = (direction == SWT.DOWN) ? srcData.width
		    * bytesPerPixel : srcData.height * bytesPerPixel;
		byte[] newData = new byte[srcData.data.length];
		int width = 0, height = 0;
		for (int srcY = 0; srcY < srcData.height; srcY++) {
		  for (int srcX = 0; srcX < srcData.width; srcX++) {
		    int destX = 0, destY = 0, destIndex = 0, srcIndex = 0;
		    switch (direction) {
		    case SWT.LEFT: // left 90 degrees
		  destX = srcY;
		  destY = srcData.width - srcX - 1;
		  width = srcData.height;
		  height = srcData.width;
		  break;
		case SWT.RIGHT: // right 90 degrees
		  destX = srcData.height - srcY - 1;
		  destY = srcX;
		  width = srcData.height;
		  height = srcData.width;
		  break;
		case SWT.DOWN: // 180 degrees
		      destX = srcData.width - srcX - 1;
		      destY = srcData.height - srcY - 1;
		      width = srcData.width;
		      height = srcData.height;
		      break;
		    }
		    destIndex = (destY * destBytesPerLine)
		        + (destX * bytesPerPixel);
		    srcIndex = (srcY * srcData.bytesPerLine)
		        + (srcX * bytesPerPixel);
		    System.arraycopy(srcData.data, srcIndex, newData, destIndex,
		        bytesPerPixel);
		  }
		}
		// destBytesPerLine is used as scanlinePad to ensure that no padding is
		// required
		return new ImageData(width, height, srcData.depth, srcData.palette,
		    destBytesPerLine, newData);
	}

}
